﻿using System;
using System.Drawing;

namespace  MaterialEditor
{
    public class MultiplyNode : Node
    {
        private InputSlot A
        {
            get { return input[0]; }
        }
        private InputSlot B
        {
            get { return input[1]; }
        }

        private NodeValue Value;

        private NodeValueType[][] _acceptTypes;
        public override NodeValueType[][] acceptTypes
        {
            get { return _acceptTypes; }
        }

        public MultiplyNode(int x, int y)
            : base(x, y)
        {
            input = new InputSlot[]
            {
                new InputSlot(this, null, "A"),
                new InputSlot(this ,null, "B"),
            };
            A.Connected += A_Connected;
            B.Connected += B_Connected;

            A.Disconnected += A_Disconnected;
            B.Disconnected += B_Disconnected;

            A.ValueChanged += A_Connected;
            B.ValueChanged += B_Connected;

            output = new InputSlot(this, null, "");

            Name = "Multiply";

            _acceptTypes = new NodeValueType[][]
            {
                new NodeValueType[] { NodeValueType.ValueColor, NodeValueType.ValueFloat, NodeValueType.ValueTexture, NodeValueType.ValueVector2, NodeValueType.ValueVector3 },
                new NodeValueType[] { NodeValueType.ValueColor, NodeValueType.ValueFloat, NodeValueType.ValueTexture, NodeValueType.ValueVector2, NodeValueType.ValueVector3 },
            };
        }

        void B_Disconnected()
        {
            if (A.isConnected)
                Value = A.connector.startSlot.value;
            else
                Value = null;
        }

        void A_Disconnected()
        {
            if (B.isConnected)
                Value = B.connector.startSlot.value;
            else
                Value = null;
        }

        void B_Connected()
        {
            try
            {
                if (A.connector != null)
                    Value = A.connector.startSlot.value * B.connector.startSlot.value;
                else
                    Value = B.connector.startSlot.value;

            }
            catch (InvalidOperationException e)
            {
                B.connector.Disconnect();
            }
        }

        void A_Connected()
        {
            try
            {
                if (B.connector != null)
                    Value = A.connector.startSlot.value * B.connector.startSlot.value;
                else
                    Value = A.connector.startSlot.value;

            }
            catch (InvalidOperationException e)
            {
                A.connector.Disconnect();
            }
        }

        public override void Update()
        {
            base.Update();
        }

        public override NodeValue Result
        {
            get { return Value; }
        }

        public override object Clone()
        {
            MultiplyNode n = new MultiplyNode((int)position.X, (int)position.Y);
            if (Result != null)
                n.Value = new NodeValue(Result.Value);

            return n;
        }
    }
}